<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/1/4
 * Time: 14:32
 */
namespace Home\Controller;
use Admin\Model\DailyRecorddetailModel;
use Admin\Model\DailyRecordindexModel;
use Admin\Model\DosemodeModel;
use Admin\Model\InspectioncontentModel;
use Admin\Model\InspectiondetailinfoModel;
use Admin\Model\InspectionIndexModel;
use Admin\Model\InspectionPicDetailModel;
use Admin\Model\PatientDosedetailModel;
use Admin\Model\PatientDoseindexModel;
use Admin\Model\PatientInfoModel;
use Admin\Model\PatientOperationInfoModel;
use Admin\Model\PrescriptionDoseDetailModel;
use Admin\Model\PrescriptionIndexModel;
use Admin\Model\TroubleDetailModel;
use Base\Controller\BaseHomeController;

class  PatientInfoController extends BaseHomeController
{
    protected $model;
    protected $upload;
    function __construct()
    {
        parent::__construct(false,true,array(
            'input_name' => 'certCover',//文本框name
            'save_path' => 'certCover',//保存路径
            'data_field' => 'certCover'//数据库字段
        ));
        $this->model = new PatientInfoModel();
    }
    /**
     * 患者详情页
     */
    public  function  detail($id){
        $PrescriptionDoseDetailModel  =  new   PrescriptionDoseDetailModel();
        $PatientDosedetailModel       =  new   PatientDosedetailModel();
        $PrescriptionIndexModel       =  new   PrescriptionIndexModel();
        $DosemodeModel                =  new   DosemodeModel();
        $DailyRecordindexModel        =  new   DailyRecordindexModel();
        $PatientOperationInfoModel    =  new   PatientOperationInfoModel();
        $DailyRecorddetailModel       =  new   DailyRecorddetailModel();
        $TroubleDetailModel           =  new   TroubleDetailModel();
        $InspectionIndexModel         =  new   InspectionIndexModel();
        $InspectioncontentModel       =  new   InspectioncontentModel();
        $InspectionPicDetailModel     =  new  InspectionPicDetailModel();
        $Inspectionid=$InspectionIndexModel->field('id,testtime')->where('userid='.$id)->order('testtime desc')->find();//患者最近一次的化验记录
        if($Inspectionid){
            $picturelist=$InspectionPicDetailModel->where('indexid='.$Inspectionid['id'])->select();//患者最近一次的化验记录图片的集合
            $Inspectionnamelist=$InspectioncontentModel->field('inspectionName,indexId')->where('indexId='.$Inspectionid['id'])->group('inspectionName')->select();//获取所有检查单不包括子项
        }
        $troubleinfolist=$TroubleDetailModel->relation(true)->where('userid='.$id)->order('logtime desc')->find();//不适症状信息
        $daily['userid']=$id;
        $daily['dailylogbegintime'] = array('EGT',date('Y-m-d 00:00:00'));
        $daily['dailylogendtime'] =array('ELT',date('Y-m-d 59:59:59'));
        $DailyRecordid=$DailyRecordindexModel->where($daily)->getField('id');//日常记录索引
        $operationInfo =$PatientOperationInfoModel->relation(true)->where('patientid='.$id)->find();//患者手术信息和个人信息
      //  dump($operationInfo);
        $indexinfo=$PrescriptionIndexModel->relation(true)->where('patientid='.$id)->order('edittime desc')->find();//查询个人最近的服药单
        if($DailyRecordid){
            $condition = new \stdClass();
            $condition->logType = 1;
            $condition->indexid= $DailyRecordid;
            $DailyRecordlist= $DailyRecorddetailModel->relation(true)->where($condition)->select();
        }
        if($indexinfo){
            $PatientDoseindexModel = new  PatientDoseindexModel();
            $map['usedatetime'] = array(array('EGT',date('Y-m-d 00:00:00')),array('ELT',date('Y-m-d 59:59:59'))) ;
            $map['userid']=$id;
            $info=$PatientDoseindexModel->where($map)->find();
            if(!$info){
                $data['userid']=$id;
                $data['status']=0;
                $data['usedatetime']=date('Y-m-d h:m:s');
                $data['plid']=$indexinfo['id'];
                $ids=$PatientDoseindexModel->add($data);
                foreach ($indexinfo['prescription_detail'] as  $k=>$v){
                    if(substr($v['periodbrgin'],0,10)<=date('Y-m-d') and substr($v['periodend'],0,10)>=date('Y-m-d')){
                        $timelen=$DosemodeModel->where('id='.$v['usetypeid'])->getField('howto');
                        $days=date('d')-substr($v['periodbegin'],8,2);
                        if($days % $timelen ==0){
                            $name=$PrescriptionDoseDetailModel->where('plid='.$v['id'])->select();
                            foreach ($name as $vv=>$kk){
                                $datas['detailid']=$ids;
                                $datas['mid']=$v['mid'];
                                $datas['usetime']=$kk['usetime'];
                                $datas['status']=0;
                                $datas['usecount']=$kk['usecount'];
                                $PatientDosedetailModel->add($datas);
                            }
                        }
                    }
                }
                $medicineid=$ids;
            }else{
                $medicineid=$info['id'];
            }
            $medicineinfo=$PatientDosedetailModel->relation(true)->where('detailid='.$medicineid)->select();
        }
        $time = date('Y-m-d');
        $this->assign('time',$time);
        $this->assign('inspectionnamelist',$Inspectionnamelist);
        $this->assign('picturelist',$picturelist);
        $this->assign('operationInfo',$operationInfo);
        $this->assign('troubleinfolist',$troubleinfolist);
        $this->assign('dailyrecordlist',$DailyRecordlist);
        $this->assign('medicinelist',$medicineinfo);
        $this->display();
    }
    /**
     * 服药记录
     */
    public  function  medcineinfo(){
        $this->display();
    }
    /**
     * 档案袋
     */
    public function healthrecord(){
        $InspectionIndexModel=new  InspectionIndexModel();
        $InspectionIndex=$InspectionIndexModel->relation(true)->where('userid='.I('userid'))->order('testtime desc')->select();
//        for ($i=4;$i<count($InspectionIndex);$i++){
//             unset($InspectionIndex[$i]['pictruelist']);
//         }
        $this->assign('inspectionindex',$InspectionIndex);
        $this->display();
   }
}